<?php

/**********************\

File     :	.php
Created  :	
Updated  :  
Author   :	
Function :	
Comments :	

\**********************/

class karajia
{
	var $users = array(array());
	var $sessionLength = 900;
	
	function nick_change_response(&$irc, &$data) {
		if(substr(strtolower($data->message), 0, 7) == 'anonyme') {
			$nick = $data->message; // because it is like that :-(
			$channel = "#serasera";
			if($nick != $irc->_nick) {
				$irc->message(SMARTIRC_TYPE_QUERY, $nick, "Efa misy tompony io solonanarana ampidainao io. Soloy hafa dia midira indray");
				$irc->message(SMARTIRC_TYPE_QUERY, $nick, "Hanova solonanarana: soraty /nick solonanarana vaovao");
				$irc->message(SMARTIRC_TYPE_QUERY, $nick, "Hiverina hikarajia: soraty /join #serasera");
				$irc->kick($channel, $nick, "Soloy ny solonanarana fa efa misy tompony.");
			}
		}
	}
    function kick_response(&$irc, &$data)
    {
		//if bot is kicked
        if ($data->nick == $irc->_nick) {
			$irc->join(array('#karajia', '#serasera'));
			$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Tsy azo dakana aho azafady :-)");
			
			Return ;
        }
	}

    function onjoin_greeting(&$irc, &$data)
    {

        // if _we_ join, greet people 
        if ($data->nick == $irc->_nick) {
			$irc->message(SMARTIRC_TYPE_QUERY, "#serasera", "Naka aina kely aho teo fa efa tafaverina indray. Soraty !vonjy raha mila fanampiana.");

            return;
        }
		//hapetraka eto koa ny ps teny ratsy
		$tenys = array();
		if($tenys = @file("data/banned.db")) {
			foreach($tenys as $key=>$val) {
				if (trim($val) != "" && @preg_match("(" . $val . ")", $data->nick)) {
					$irc->kick($data->channel, $data->nick, "Tsy ekena ny solonanaranao.");
					return;
				}
			}
		}
        // now check if the user is not yet greeted in last 24 hours
        if ($this->users[$data->nick]['time'] + 86400 < time()) {
			if($files = @file("data/arahaba.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			$this->users[$data->nick]['time'] = time();
			return;
		}

		

    }
	
	function hasAccess($nickhandle, $level)
	{
		// Check that they are in fact logged in
		if ($this->users[$nickhandle]['time'] + $this->sessionLength >= time())
		{
			// Session not expired. They are logged in.
			if ($this->users[$nickhandle]['level'] >= $level)
			{
				$this->users[$nickhandle]['time'] = time();
				return true;
			}
		}
		return false;
	}

	function wsdlAuthenticateUser( $user, $pass )
	{

		require_once("lib/nusoap.php");

		$endpoint = "http://serasera.org/wsdl/login/?wsdl";

		$client = new soapclient($endpoint, true);
		$err = $client->getError();

		
		if($err) {
			Return false;
		}

		$param = array('wsid' => "Karajia", 'username' => $user, 'password' => $pass);
		$result = $client->call('getUser', $param);
		

		if ($client->fault) {
			Return false;
		} else {
			Return ($result[loggedin] == true);
		}
	}

    function channel_response(&$irc, &$data)
    {
		if(trim($data->message) == "!login") {
			if($files = @file("data/loginvonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}
		if(substr($data->message, 0, 6) == "!vonjy") {
			if($files = @file("data/vonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}
		if(substr($data->message, 0, 7) == "!vaovao") {
			if($files = @file("data/vaovao.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			} else {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Misy olana" );
			}
			return;
		}
		if(substr($data->message, 0, 4) == "!rvg") {
			if($files = @file("data/rvgvonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}

		if(substr($data->message, 0, 10) == "!ohabolana") {
			if($files = @file("data/ohabolanavonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}

		if(substr($data->message, 0, 1) == "!") {
			$files = @file("data/ktopic.db");
			
			shuffle($files);
			foreach($files as $key=>$val) {
				$str = explode(" ", $val, 2);
				$pattern = str_replace("+", " ", $str[0]);
				if (@preg_match("(^\\" . $pattern . ")", $data->message, $result)) {
					
					$reponse = str_replace("N~", $data->nick, $str[1]);
					$reponse = str_replace("X~", $data->messageex[1], $reponse) ;

					//action sa message?
					if((trim($data->message) != trim($result[0])) && ($data->messageex[1] == "")) {
						$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Ho an'iza io " . $data->messageex[0] . " io ry " . $data->nick . "?");
					} elseif(substr($str[1], 0, 1) == "+") {
						$irc->message(SMARTIRC_TYPE_ACTION, $data->channel, substr($reponse, 1)  );
					} else {
						$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $reponse );
					}
					break;
				}
			}
			return;
		} else {
			// sivana teniratsy
		
			if($tenys = @file("data/teniratsy.db")) {
				foreach($tenys as $key=>$val) {
					if (trim($val) != "" && @preg_match("(" . $val . ")", $data->message)) {
						$irc->kick($data->channel, $data->nick, "Any ivelany ny mpiteny ratsy.");
					}
				}
			}
			return;

		}


    }

    function query_response(&$irc, &$data)
    {
		if(substr($data->message, 0, 1) == "!" && !$irc->isJoined("#serasera", $data->nick)) {
			$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy afaka manome baiko eto ianao raha tsy mikarajia ao amin'ny #serasera. Soraty hoe /join #serasera" );
			return;
		}
		$nickhandle = md5($data->nick . $data->host);
		$not_registered = "Tsy afaka mampiasa io baiko io ianao raha tsy mbola nampiasa ny baiko !login";

		//
		//zava-baovao
		if(substr($data->message, 0, 7) == "!vaovao") {
			if($files = @file("data/vaovao.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}


		//fanolorana ny tolotra
		if(trim($data->message) == "!tolotra") {
			if(!$this->hasAccess($nickhandle, 1)) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy afaka mahita ny tolotra ianao raha tsy mbola nampiasa ny baiko !login");
				
			} else {
				if($files = @file("data/tolotravonjy.db")) {
					foreach($files as $key=>$val) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
					}
				}
			}			
			return;
		}

		//logout
		if(trim($data->message) == "!logout") {
			$this->users[$nickhandle]['time'] = 0;

			$level = 1;
			$this->users[$nickhandle]['level'] = 0;
			$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tafavoaka soa amantsara ianao. Raha hiverina hiditra dia soraty ny baiko !login.");
			return;
		}
		//login
		if(trim($data->message) == "!login") {
			if($files = @file("data/loginvonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}
		
		if(substr($data->message, 0, 6) == "!login") {
			if($data->messageex[1] && $data->messageex[2]) {
				$username = $data->messageex[1];
				$password = $data->messageex[2];
				if($userArray = $this->wsdlAuthenticateUser( $username, $password )) {
					$this->users[$nickhandle]['alias'] = $data->nick;
					$this->users[$nickhandle]['host'] = $data->host;
					$this->users[$nickhandle]['username'] = $username;
					$this->users[$nickhandle]['time'] = time();

					$level = 1;
					$this->users[$nickhandle]['level'] = $level;
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tafiditra soa amantsara ianao. Raha mitovy amin'ny solonanarana entinao ao amin'ny serasera ny solonanarana entinao eto dia miakatra ho VOICE ianao.");
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Raha vao tsy mikarajia ao anatin'ny 15 minitra ianao dia tsy maintsy mampiditra solonanarana sy tenimiafina indray.");
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Soraty hoe !tolotra hahitanao ny zavatra atolotra ho anao.");

					if($data->nick == $username) {
						$irc->voice("#serasera", $data->nick );
					}
					

					//log
					$logmsg = gmdate("d/m/Y H:i") . "\t" . $data->nick . " \t " . $username . "\t" . $data->host;

					//log
					$fp = @fopen("log/karabot" . (gmdate("d/m/Y")) . ".log", 'a');
					@fwrite($fp, $logmsg);
					@fclose($fp);
				} else {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Diso ny solonanarana na ny tenimiafina. Avereno indray.");
				}
			} else {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Mba hahafahanao miditra mampiasa ireo tolotra isan-karazany dia soraty 3!login solonanarana tenimiafina");
			}
			Return ;
		}
		
		if(substr($data->message, 0, 6) == "!vonjy" ) {
			if($files = @file("data/vonjy.db")) {
				foreach($files as $key=>$val) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
				}
			}
			return;
		}
		

		//ohabolana
		if(substr($data->message, 0, 10) == "!ohabolana" ) {
			if(!$this->hasAccess($nickhandle, 1)) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $not_registered );
				Return ;
			}
			$endpoint = "http://serasera.org/ws/ohabolana/?wsdl";
			require_once("lib/nusoap.php");

			$client = new soapclient($endpoint, true);
			$err = $client->getError();

			
			if($err) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay ohabolana aho... angamba misy olana. Erreur 315");
				return;
			}
			

			$filter = $data->messageex[1];
			
			$param = array('wsid' => "Karajia", 'filter' => $data->messageex[1]);
			$result = $client->call('getQuote', $param);
			if ($client->fault) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay ohabolana aho... angamba misy olana. Erreur 325");
				return;
			}
			if($result) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $result);
				return;
			} else {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Nisy hadisoana, tsy hita ny ohabolana");
				return;
			}
			Return ;
		}


		//
		//Radiovazogasy
		if(substr($data->message, 0, 4) == "!rvg" ) {
			if($data->message == "!rvg") {
				//giving help
				if($files = @file("data/rvgvonjy.db")) {
					foreach($files as $key=>$val) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $val );
					}
				}
				return;
			}
			if(!$this->hasAccess($nickhandle, 1)) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $not_registered );
				Return ;
			}
			$endpoint = "http://radiovazogasy.com/wsdl/?wsdl";
			require_once("lib/nusoap.php");

			$client = new soapclient($endpoint, true);
			$err = $client->getError();

			
			if($err) {
				$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
				return;
			}
			

			//rvg hafatra
			
			if($data->messageex[0] == "!rvg_hafaro") {
				if($data->messageex[1] == "" || $data->messageex[2] == "") {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Diso ny baiko napetrakao. Ny baiko marina dia !rvg_hafaro laharana hafatra (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg_miandry");
					Return ;
				} elseif(intval($data->messageex[1]) == 0) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Diso ny baiko napetrakao. Ny baiko marina dia !rvg_hafaro laharana hafatra (ka ny laharana dia ilay laharan'ny hira ao amin'ny !rvg_miandry");
					Return ;
				} else {
					$msg = "";
					for($ii=2; $ii<count($data->messageex)-1; $ii++) {
						$msg .= $data->messageex[$ii] . " ";
					}
					$param = array('wsid' => "Karajia", 'id' => intval($data->messageex[1]), 'username' => $this->users[$nickhandle]['username'], 'msg' => $msg);
					$result = $client->call('rvgHafaro', $param);
					if ($client->fault) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay na inona na inona aho... angamba tsy mandeha ny RadioVazoGasy. ");
						return;
					}
					if($result != "") {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $result);
						return;
					} else {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Nisy hadisoana, tsy lasa ny hafatra");
						return;
					}
					Return ;
				}
				
			}


			//handahatra hira
			
			if($data->messageex[0] == "!rvg_alaharo") {
				if($data->messageex[1] == "") {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy nametraka ny laharan'ny hira halahatra ianao. Soraty !rvg_tadiavo raha hitady hira");
					Return ;
				} elseif(intval($data->messageex[1]) == 0) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy laharana ara-dalana io napetrakao io.");
					Return ;
				} else {
					$param = array('wsid' => "Karajia", 'id' => intval($data->messageex[1]), 'username' => $this->users[$nickhandle]['username'] . "[karajia]");
					$result = $client->call('rvgAlaharo', $param);
					if ($client->fault) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy. ");
						return;
					}
					if($result) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Valiny: " .$result );
						return;
					} else {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy tafiditra ny hira... Nisy olana");
						return;
					}
				}
				
			}

			//hitady hira
			if($data->messageex[0] == "!rvg_tadiavo") {
				if($data->messageex[1] == "") {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy nametraka lohateny na mpihira hotadiavina ianao. Soraty ny baiko !rvg_tadiavo teny tadiavina");
					Return ;
				} else {
					$tadiavo = trim(substr($data->message, strlen($data->messageex[0])+1));
					$param = array('wsid' => "Karajia", 'tadiavo' => $tadiavo);
					$results = $client->call('rvgTadiavo', $param);
					if ($client->fault) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
						return;
					}
					if(is_array($results)) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Ireto ny hira hita. Izay misy isa dia azonao asiana alahatra amin'ny baiko !rvg_alaharo");
						foreach($results as $result) {
							$msg = $result[artist] . ", " . $result[title] . "(" . $result[length] . ") , 4!rvg_alaharo " . $result[id] ; 
							$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $msg);
						}
						return;
					} else {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
						return;
					}
				}
			}

			//hira miandry
			if(trim($data->messageex[0] == "!rvg_miandry")) {
				$param = array('wsid' => "Karajia");
				$results = $client->call('rvgHandeha', $param);
				if ($client->fault) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
					return;
				}

				if(is_array($results)) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Ireto ny hira handeha rehefa avy eo. Izay misy isa dia azonao asiana hafatra amin'ny baiko !rvg_hafaro");
					foreach($results as $result) {
						$msg = $result[artist] . ", " . $result[title] . "(" . $result[length] . ") ";
						if($result[requester]) {
							$msg .= "- 3Nalahatr'i : " . $result[requester];
						}
						if($result[id] > 0 && $result[msg] == "") {
							$msg .= ", 4!rvg_hafaro " . $result[id] ;
						} 
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $msg);
					}
					return;
				} else {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy misy hira voaray.");
					return;
				}
			}
			if(trim($data->messageex[0]) == "!rvg_hira") {
				//hira mandeha


				$param = array('wsid' => "Karajia");
				$result = $client->call('rvgHira', $param);
				

				if ($client->fault) {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy maharay hira aho... angamba tsy mandeha ny RadioVazoGasy.");
					return;
				} else {
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Hira mandeha");
					$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Lohateny: 3" . $result[title]);
					if(trim($result[album]) != "") {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Rakipeo: 3" . $result[album]);						
					}
					if(trim($result[requester]) != "") {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Nangataka: 3" . $result[requester]);						
					}
					if(trim($result[username]) != "") {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Nametraka hafatra: 3" . $result[username]);						
					}
					if(trim($result[msg]) != "") {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Hafatra: 3" . $result[msg]);						
					}
					return;
				}

				Return ;
			}

		}
		
		//user list
		if($data->message == "!list" && ($data->nick == "hery" || $data->nick == "lanto")) {
			$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Lisitry ny namana voatahiry.");
			if(is_array($this->users)) {
				foreach($this->users as $users) {
					if($users['alias']) {
						$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, $users['alias'] . " - " . $users['username'] . " - " . $users['host'] . " - " . (gmdate("d/m/Y H:i", $users['time'])));
					}
				}
			}
			Return ;
		}

		//quit
		if($data->message == "!restart" && ($data->nick == "hery" || $data->nick == "lanto" || $data->nick == "Fa" || $data->nick == "Angie")) {
			$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Hihidy i Botlives.");
			exit();
			Return ;
		}


		if($data->messageex[0] == "!send" && ($data->nick == "hery" || $data->nick == "lanto")) {
			if($data->messageex[1] && $data->messageex[2]) {
				$message = "";
				for($ii=2; $ii<=count($data->messageex) - 1; $ii++) {
					$message .= $data->messageex[$ii] . " ";
				}
				$irc->message(SMARTIRC_TYPE_CHANNEL, $data->messageex[1], $message);
				
			}
			Return ;
		}


		//default
		if(substr($data->message, 0, 1) == "!" ) {
			$irc->message(SMARTIRC_TYPE_QUERY, $data->nick, "Tsy mandeha eto io baiko io. Soraty ny baiko !vonjy raha mila fanampiana.");
			return;
		}

    }
}








///---------
$bot = &new karajia();
$irc = &new Net_SmartIRC();
$irc->setDebug(SMARTIRC_DEBUG_NOTICE);
//$irc->setDebug(SMARTIRC_DEBUG_ALL);
$irc->setLogdestination(SMARTIRC_FILE);
$irc->setLogfile("/home/hera/karabot/log/log_" . date("YmdHm") . ".log"); 
$irc->setAutoReconnect(TRUE);
$irc->setChannelSyncing(TRUE);
$irc->setUseSockets(TRUE);
$irc->registerActionhandler(SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, '.*', $bot, 'query_response');
$irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL, '.*', $bot, 'channel_response');
$irc->registerActionhandler(SMARTIRC_TYPE_JOIN, '.*', $bot, 'onjoin_greeting');
$irc->registerActionhandler(SMARTIRC_TYPE_KICK, '.*', $bot, 'kick_response');
$irc->registerActionhandler(SMARTIRC_TYPE_NICKCHANGE, '.*', $bot, 'nick_change_response');
$irc->connect('shell.thehostbusters.com', 32500);
$irc->login('BotLives01', 'Karajia serasera '.SMARTIRC_VERSION.' 
(karajia.php)', 0, 'BotLives01', 'BWANA');
$irc->join(array('#karajia', '#serasera'));
$irc->message(SMARTIRC_TYPE_QUERY, 'Themis', "IDENTIFY BWANA");						

$irc->listen();
$irc->disconnect();


?>
